<?php
/**
 * 用户行为处理类
 * 必须依赖MyPDO类、Session功能，可选依赖MyLog类
 * @author FLY
 *
 */
class MemberHelper
{
    static $password_key = '9B389856B627CF58F77E2F02C3695DD0';
    static $remember_time = 2592000; // 30天
    static $remember_key = '8C4595AD299C1EED9C34C67F5F846290';
    static $error_interval = 10800; // 允许密码错误间隔：3小时
    static $error_times = 5; // 允许密码错误次数，超过则拒绝登陆
    static $csrf_key = 'CA969A1BC97732D97B1E88CE8396C216';    
    static function check_member($username, $type = 1)
    {
        switch (( int ) $type)
        {
            case 2 :
                $field = 'mobile';
                break;
            case 3 :
                $field = 'email';
                break;
            default :
                $field = 'username';
        }
        $db = MyPDO::get_instance();
        $db->select( 'member', '*', "`$field`=?", $username );
        $db->limit( 1 );
        return $db->fetch_cell();
    }
    static function modify_password($uid, $old_password, $new_password)
    {
        $old_password = self::encrypt_password( $old_password );
        $new_password = self::encrypt_password( $new_password );
        
        $db = MyPDO::get_instance();
        $db->update( 'member', array (
                'password' => $new_password 
        ) );
        $db->where( '`id`=? and `password`=?', array (
                $uid,
                $old_password 
        ) );
        return $db->affected_rows();
    }
    static function encrypt_password($password)
    {
        return sha1( self::$password_key . $password );
    }
    static function clean($uid)
    {
        $db = MyPDO::get_instance();
        $db->delete( 'member_login', 'add_time<? or uid=?', array (
                $_SERVER['REQUEST_TIME'] - self::$remember_time,
                $uid 
        ) );
        return $db->execute();
    }
}